package com.ear.orange.controller;

import com.ear.orange.common.Result;
import com.ear.orange.entity.AddressBook;
import com.ear.orange.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 地址簿管理
 *
 * @author orange
 */
@Api(tags = "地址簿管理")
@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 新增
     */
    @ApiOperation("添加地址簿信息")
    @PostMapping
    public Result<AddressBook> save(@RequestBody AddressBook addressBook) {
        addressBookService.setUserAddress(addressBook);
        return Result.success("添加成功");
    }

    /**
     * 设置默认地址
     */
    @PutMapping("default")
    @ApiOperation("设置默认地址")
    public Result<AddressBook> setDefault(@RequestBody AddressBook addressBook) {
        addressBookService.setDefaultAddress(addressBook);
        return Result.success(Result.UPDATE_SUCCESS);
    }

    /**
     * 根据id查询地址
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询地址")
    public Result get(@PathVariable Long id) {
        return addressBookService.getAddressById(id);
    }

    /**
     * 查询默认地址
     */
    @GetMapping("default")
    @ApiOperation("查询默认地址")
    public Result<AddressBook> getDefault() {
        return addressBookService.getDefaultAddress();
    }

    /**
     * 查询指定用户的全部地址
     */
    @GetMapping("/list")
    @ApiOperation("查询指定用户的全部地址")
    public Result<List<AddressBook>> list() {
        return addressBookService.getUserAddressList();
    }

    @PutMapping()
    @ApiOperation("修改地址信息")
    public Result updateInfo(@RequestBody AddressBook addressBook){
        return addressBookService.updateInfo(addressBook);
    }
}
